;( function () {
	"use strict";
    var param_map = Result.getLocationParamObject();
    var global_param_map;
	
	$( function () {
		if(!ifUser()){
			return;
		}
		
		queryKeepOffersList();
		
		//取消收藏
		$("#confirm").click(function(){
			var shopId = $(this).attr("shopId"); 
			var params = {
				"userId" : 	USER.userId,
				"offerId" : shopId,
			};
			$.ajax(getAjaxOps('/offer/removeKeep', params)
			).done(function(res){
	            if(res.resultCode == 0) {
	            	$("body").tips("取消成功");
	            	queryKeepOffersList();
	            	$("#confirm").attr("shopId", "");
	            	$('#cancelorder-confirm').modal('close');
	            } else {
	            	$("body").tips(res.data);
	            }
		    });
		});
		
	}); 

	/**
	 * 收藏列表
	 */
	function queryKeepOffersList(){
		var params = {
			"userId" : 	USER.userId,
		};
		var $template = $( "#template" ),
       		$list = $( "#list" );
		
		$.ajax(getAjaxOps('/offer/qryKeepOffersList', params)
		).done(function(res){
			var data = res.data;
            if(res.resultCode == 0) {
            	$list.empty();
                if( $.isArray( data ) ) {
                	for( var i = 0, l = data.length; i < l; i++ ) {
               			var obj = $template.clone().removeAttr( "id" );
                    	$list.append( Result.fillData( obj, data[i], "field" ));
                    }
               	}
                init();
            } else {
            	$("body").tips(res.data);
            }
	    });
	}
	
	function init(){
		$("#list").find(".to_order_order").click(function(){
			var shopId = $(this).attr("shopId");
			Result.forward("detail.html", {"goodId" : shopId});
			return;
		});
		
		$("#list").find(".un_collect").click(function(){
			var shopId = $(this).attr("shopId");
			$("#confirm").attr("shopId", shopId);
			$('#cancelorder-confirm').modal('open');
			return;
		});
	}
	
	
	window.replace_photo = function ( $field, photo, rowVal ) {
		$field.attr("src", IMG_PRE + photo);
	};
	
	window.replaceId = function ( $field, id, rowVal ) {
		$field.attr("shopId", id);
	};
	
	window.replace_flag = function ( $field, flag, rowVal ) {
		var flag = rowVal['flag'],
			inPrice = rowVal['in_price'],
			outPrice = rowVal['out_price'];
		if(flag != 0){
			$field.html("" + outPrice);
		} else {
			$field.html("" + inPrice);
		}
	};
	
	
	
	
	/**
	 * 跳到首页
	 */
	window.toIndex = function(){
		Result.forward("index.html", {});
	};
	
	/**
	 * 跳到用户信息
	 */
	window.toUserInfo = function(){
		Result.forward("userInfos.html", {});
	};
	
	

}( window ) );
